﻿using Nemerle.Collections;
using Nemerle.Text;
using Nemerle.Utility;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Nemerle.Xml;
using System.IO;

namespace DocBrowser.Config
{
  /// <summary>
  /// Description of Root.
  /// </summary>
  [Record]
  class Settings
  {
    public LoadedAssemblyPaths  : list[string] { get; }
    public CurrentSelectionPath : list[string] { get; }
    
    public this()
    {
      LoadedAssemblyPaths  = [];
      CurrentSelectionPath = [];
    }
    
    public this(path : string)
    {
      def xmlDoc = if (File.Exists(path)) XDocument.Load(path) else null;
      
      if (xmlDoc == null)
        this()
      else
        this(xmlDoc)
    }
    
    public this(xmlDoc : XDocument)
    {
      def root = xmlDoc.Element("Settings");
      LoadedAssemblyPaths = root?.Element("LoadedAssemblyPaths")?.Elements("Paths")?.MapToList(e => e.Value) ?? [];
      CurrentSelectionPath = root?.Element("CurrentSelectionPath")?.Value?.SplitToList('\\') ?? [];
    }

    public ToXml() : XDocument
    {
      XDocument(xml <#
<Settings>
  <LoadedAssemblyPaths>
    <Paths $foreach (p in LoadedAssemblyPaths)>$p</Paths>
  </LoadedAssemblyPaths>
  <CurrentSelectionPath>$($<#..$(CurrentSelectionPath; @"\")#>)</CurrentSelectionPath>
</Settings>
        #>)
    }    
  }
}
